<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/5/6
 * Time: 10:46 AM
 */

class _PostManNodeRequestUrlItem {
    var $raw;
    var $protocol;
    var $host; //arr
    var $path; //arr
}

class _PostManNodeRequestBodyItem {
    var $mode;
    var $raw;
}

class _PostManNodeRequestItem {
    var $method;
    var $header; //arr
    var $body; //_PostManNodeRequestBodyItem
    var $url; //_PostManNodeRequestUrlItem
}

class PostManNodeItem {
    var $name;
    var $item;
    var $request; // _PostManNodeRequestItem
    var $response; // Arr

    var $_postman_isSubFolder;
}

// 遍历目录
define("MAX_DEPTH", 3);
define("BASE_URL", "http://phplearningloc/broshop/");
define("HOST", "phplearningloc");
define("BASE_PATH", "broshop");
$baseUrl = "http://phplearningloc/broshop/";
$dirname = "../broshop";
$dirHandle = opendir($dirname);

ini_set('memory_limit','256M'); //升级为256M内存

function readContentFromDirRecursively($dir, &$contentsArray, $subFolder, $depth=0) {
    if ($depth > MAX_DEPTH) return;
    $dirHandle = opendir($dir);
    while ($fileName = readdir($dirHandle)) {
        if ($fileName == "." || $fileName == ".." || is_link($fileName)){
            continue;
        }
        $subFilePath= $dir . DIRECTORY_SEPARATOR . $fileName;
        if (is_file($subFilePath)) {

            if (!strstr($fileName, ".html")
                && !strstr($fileName, ".htm")
                && !strstr($fileName, ".php")) {
                continue;
            }

            // 处理文件
            $fileItem = new PostManNodeItem();
            $fileItem->name = $fileName;

            // Request
            $request = new _PostManNodeRequestItem();
            $request->method = "GET";
            $request->header = array();

            // BODY
            $body = new _PostManNodeRequestBodyItem();
            $body->mode = "raw";
            $body->raw = "";

            // URL
            $url = new _PostManNodeRequestUrlItem();
            $url->raw = BASE_URL . $subFilePath;
            $url->protocol = "http";
            $url->host = array();
            array_push($url->host, HOST);
            $url->path = array();
            array_push($url->path, BASE_PATH);
            // 添加目录到PATH
            foreach ($subFolder as $index => $folder) {
                array_push($url->path, $folder);
            }
            // 添加文件名到PATH
            array_push($url->path, $fileName);

            $request->body = $body;
            $request->url = $url;
            $fileItem->request = $request;

            array_push($contentsArray, $fileItem);

        } else if (is_dir($subFilePath) && !is_link($subFilePath)) {
            $dirItem = new PostManNodeItem();
            $dirItem->name = $fileName;
            $dirItem->item = array();
            $dirItem->_postman_isSubFolder = "true";
            array_push($contentsArray, $dirItem);

            // 目录结构
            $folders = array();
            foreach ($subFolder as $index => $folder) {
                array_push($folders, $folder);
            }
            array_push($folders, $fileName);

            // 处理文件夹
            readContentFromDirRecursively($subFilePath, $dirItem->item, $folders, $depth+1);
        }
    }
}

$allContentsArray = array();
$allSubFolders = array();
readContentFromDirRecursively($dirname, $allContentsArray, $allSubFolders);

//print_r($allContentsArray);
array_multisort(array_column($allContentsArray, 'name'), SORT_ASC, $allContentsArray);

$json = json_encode($allContentsArray);
echo $json;


